home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 47 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  111 lines

  1. Path: io.org!singhr
  2. From: singhr@io.org (Robert Harold Singh)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: infix to postfix ???
  5. Date: 2 Jan 1996 05:47:59 GMT
  6. Organization: Internex Online, Toronto, Ontario, Canada (416 363 3783)
  7. Message-ID: <4caguf$1nl@ionews.io.org>
  8. References: <4b8gve$mu0@db.csie.ncu.edu.tw>
  9. NNTP-Posting-Host: twirl.io.org
  10. X-Newsreader: TIN [version 1.2 PL2ovr]
  11.  
  12. 83085400 (wzyang@csie.ncu.edu.tw) wrote:
  13.  
  14.  
  15. :   Can you help me debug the program ? I need you help !
  16. : inupt : input a infix  contain {} [] () parentheses
  17. : output : use stack calculate postfix value 
  18. : exampleíG  2*(4+3*2-5)
  19.  
  20. Here's a program I wrote the other day that converts an infix string at
  21. argv[1] to postfix notation..
  22.  
  23. Actually, I think the way I did it is called reverse postfix notation.
  24. Damn school is always switching terminology on me.
  25.  
  26. The program may have a few bugs, but hell.. it's my third C program :)
  27. Btw.. your program is WAAAAY complex than it need be.
  28.  
  29. /*
  30.  
  31.    infix -> postfix notation converter
  32.  
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <string.h>
  37.  
  38. main (int argc, char **argv)
  39. {
  40.   if (argc != 2)
  41.     {
  42.       puts ("usage: postfix infix_equation");
  43.       return 1;
  44.     }
  45.   char *oper = "(+-*/^";
  46.   char *doh = ")";
  47.   int c, i, j, sp, prio[] = {0, 1, 1, 2, 2, 3};
  48.   char output[20];
  49. /*
  50.    allocate memory for our stacks by parsing argv[1] and tallying
  51.    the number of operators in the string
  52.  */
  53.   for (c = i = sp = 0; unsigned(i) <= strlen (argv[1]); i++)
  54.     {
  55.       j = 0;
  56.       while ((argv[1][i] != oper[j]) && (unsigned(j) < strlen (oper)))
  57.         j++;
  58.       if (argv[1][i] == oper[j])
  59.         c++;
  60.     }
  61.   char ssOper[c], ssPrio[c];
  62.  
  63.   for (i = c = 0; unsigned(i) < strlen (argv[1]); i++)
  64.     {
  65.       j = 0;
  66.       while ((argv[1][i] != oper[j]) && (unsigned(j) < strlen (oper)))
  67.         j++;
  68.       if ((unsigned(j) >= strlen (oper)) && (argv[1][i] != doh[0]))
  69.         {
  70.           output[c] = argv[1][i];
  71.           c++;
  72.      }
  73.       else if (argv[1][i] != doh[0])
  74.         {
  75.           while ((sp >= 0) && (ssPrio[sp] >= prio[j]) && (j > 0))
  76.             {
  77.               output[c] = ssOper[sp];
  78.               sp--;
  79.               c++;
  80.             }
  81.           if ((j == 0) || (sp == -1) || (ssPrio[sp] < prio[j]))
  82.             {
  83.               sp++;
  84.               ssOper[sp] = oper[j];
  85.               ssPrio[sp] = prio[j];
  86.             }
  87.         }
  88.       else
  89.         while ((sp >= 0) && (ssOper[sp] != oper[0]))
  90.           {
  91.             output[c] = ssOper[sp];
  92.             c++;
  93.             sp--;
  94.           }
  95.     }
  96.   while (sp > 0)
  97.     {
  98.       if (ssOper[sp] != oper[0])
  99.         {
  100.           output[c] = ssOper[sp];
  101.           c++;
  102.         }
  103.       sp--;
  104.     }
  105.   output[c] = 0;
  106.   printf ("infix: %s, postfix: %s\n", argv[1], output);
  107. }
  108.  
  109. -- Robert H Singh
  110. singhr@io.org
  111.